package com.example.zyfypt_206shj.model;

import com.example.zyfypt_206shj.bean.UserInfo;
import com.example.zyfypt_206shj.iface.LoginIface;
import com.example.zyfypt_206shj.iface.LoginListener;
import com.example.zyfypt_206shj.service.LoginService;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;
import retrofit2.converter.scalars.ScalarsConverterFactory;

/**
 * Created by Raindy on 2017/4/14.
 */

public class LoginModel implements LoginIface {
    private Retrofit retrofit;
    private String BASEURL="http://amicool.neusoft.edu.cn/";
    private LoginService loginService;

    public LoginModel() {
        //使用Retrofit-1
        retrofit = new Retrofit.Builder().baseUrl(BASEURL).addConverterFactory(GsonConverterFactory.create()).build();
    }

    @Override
    public void getUserInfo(String username, String password, final LoginListener listenser) {
        //使用Retrofit-2
        loginService = retrofit.create(LoginService.class);
        Call<UserInfo> call = loginService.login(username, password);
        //使用Retrofit-3
        call.enqueue(new Callback<UserInfo>() {
            @Override
            public void onResponse(Call<UserInfo> call, Response<UserInfo> response) {
                if(response.isSuccessful() && response.body()!=null){
                    listenser.onResponse(response.body());
                }else {
                    listenser.onFail("onResponse fail.");
                }
            }

            @Override
            public void onFailure(Call<UserInfo> call, Throwable t) {
                listenser.onFail(t.toString());
            }
        });
    }
}

